class Solution {
public:
    vector<vector<int> > fourSum(vector<int> &num, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        sort(num.begin(), num.end());
        vector<int> record(4);
        vector<vector<int> > ans;

        for (int i = 0; i < n; ) {
            for (int j = i + 1; j < n; ) {
                for (int k = j + 1, l = n - 1; k < n; ) {
                    while (k < l && num[i] + num[j] + num[k] + num[l] > target) {
                        --l;
                    }
                    if (k == l) {
                        break;
                    }
                    if (num[i] + num[j] + num[k] + num[l] == target) {
                        record[0] = i;
                        record[1] = j;
                        record[2] = k;
                        record[3] = l;
                        ans.push_back(record);
                    }
                    int x = k + 1;
                    while (x < n && num[x] == num[k]) {
                        ++x;
                    }
                    k = x;
                }
                int x = j + 1;
                while (x < n && num[x] == num[j]) {
                    ++x;
                }
                j = x;
            }
            int x = i + 1;
            while (x < n && num[x] == num[i]) {
                ++x;
            }
            i = x;
        }

    }
};
